/*
 * 文件名               :  BridgeConnector.java
 * 用途                    :  桥连接器,持有2个线程
 * 最后修改日期:  $LastChangedDate: 2009-10-17 03:54:49 +0000 (Sat, 17 Oct 2009) $
 * 最后修改者     :  $LastChangedBy: wendal1985@gmail.com $
 * 创建者               :  Wendal Chen <wendal1985@gmail.com>
 */
package com.wendal.java.preproxy.bridge;

import java.io.IOException;
import java.net.Socket;

import com.wendal.java.preproxy.bean.MessageBean;
import com.wendal.java.preproxy.server.SocketHandlerFinder;
import com.wendal.java.preproxy.thread.DataReaderThread;
import com.wendal.java.preproxy.thread.DataTransferThread;

public class BridgeConnector{
	
	private DataTransferThread dataTransferThread;
	
	private DataReaderThread dataReaderThread;
	
	
	public BridgeConnector(Socket socket, ByteArrayHandler handler,SocketHandlerFinder socketHandlerFinder) throws IOException {
		dataTransferThread =  new DataTransferThread(socket.getOutputStream());
		
		dataTransferThread.start();
		dataReaderThread = new DataReaderThread(socket.getInputStream(),socketHandlerFinder,handler);
		dataReaderThread.start();
		BridgeConnector.connector = this;
	}

	public synchronized void transfer(MessageBean messageBean){
		if(messageBean == null) return;
		dataTransferThread.addMessageBean(messageBean);
	}
	
	public boolean isAlive(){
		return dataReaderThread.isAlive() && dataTransferThread.isAlive();
	}
	
	private static BridgeConnector connector;
	
	public static BridgeConnector getDefaultBridgeConnector(){
		return connector;
	}
}
